package com.sp.system.user.core.utils;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @Describle 日期转换工具类
 * @author chenanhai
 * @Date 2019年10月8日
 */
public class DateUtil {

    private static String dateFormat = "yyyy-MM-dd HH:mm:ss";

    public static Date getNowDate(){
        return new Date();
    }
    public static String getNowStrDate(){
        return datePatternToStr(LocalDateTime.now(),dateFormat);
    }

    public static Date strToDate(String strDate){
        Date date = strPatternToDate(strDate,dateFormat);
        return date;
    }
    public static String dateToStr(LocalDateTime localDateTime){
        String dateStr = datePatternToStr(localDateTime,dateFormat);
        return dateStr;
    }

    public static String dateToStr(Date date){
        Instant instant = date.toInstant();//An instantaneous point on the time-line.(时间线上的一个瞬时点。)
        ZoneId zoneId = ZoneId.systemDefault();//A time-zone ID, such as {@code Europe/Paris}.(时区)
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        String dateStr = datePatternToStr(localDateTime,dateFormat);
        return dateStr;
    }

    public static String dateDashToStr(LocalDateTime localDateTime){
        String date = datePatternToStr(localDateTime,"yyyyMMddHHmmss");
        return date;
    }

    public static Date strDashToDate(String strDate){
        Date date = strPatternToDate(strDate,"yyyyMMddHHmmss");
        return date;
    }

    private static Date strPatternToDate(String strDate,String pattern){
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern);
        LocalDateTime localDateTime = LocalDateTime.parse(strDate, fmt);
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        Date date = Date.from(instant);
        return date;
    }

    private static String datePatternToStr(LocalDateTime localDateTime,String pattern){
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(dateFormat);
        String dateStr = localDateTime.format(fmt);

        return dateStr;
    }

}
